View Javadoc
1 //============================================================================== 2 // file : NewsCMD.java 3 // project: East Networks News System 4 // 5 // last change: date: $Date: 2003/09/10 09:28:37 $ 6 // by: $Author: bitiboy $ 7 // revision: $Revision: 1.1 $ 8 //------------------------------------------------------------------------------ 9 // copyright: GNU GPL Software License (see class documentation) 10 //============================================================================== 11 package net.eastol.news.command; 12 13 14 /* 15 * $Id: NewsCMD.java,v 1.1 2003/09/10 09:28:37 bitiboy Exp $ 16 * 17 * Copyright 2003 Acai Software All Rights Reserved. 18 * 19 * This file NewsCMD.java is part of the East Networks News System. 20 21 * The East Networks News System is free software; you can redistribute it and/or modify 22 * it under the terms of the GNU General Public License as published by 23 * the Free Software Foundation; either version 2 of the License, or 24 * (at your option) any later version. 25 26 * East Networks News System is distributed in the hope that it will be useful, 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 29 * GNU General Public License for more details. 30 31 * You should have received a copy of the GNU General Public License 32 * along with the East Networks News System; if not, write to the Free Software 33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 34 35 * http://www.justhis.com http://ejb.cn 36 * CONTACT: email = webmaster@justhis.com superaxis@sohu.com 37 */ 38 import com.justhis.jdo.JDOUtil; 39 40 import com.justhis.jibx.JibxUtil; 41 42 import com.justhis.lucene.LuceneException; 43 import com.justhis.lucene.xml.IndexFiles; 44 45 import com.justhis.util.exception.LogicException; 46 import com.justhis.util.exception.UtilException; 47 48 import net.eastol.news.common.CommonCommand; 49 import net.eastol.news.jdo.bean.NewsBean; 50 import net.eastol.news.jdo.bean.NewsBeanBase; 51 import net.eastol.news.jdo.bean.NewsClass; 52 import net.eastol.news.jdo.bean.NewsSys; 53 import net.eastol.news.jibx.bean.NewsCommonXML; 54 import net.eastol.news.jibx.bean.PageBean; 55 56 import org.apache.lucene.analysis.Analyzer; 57 import org.apache.lucene.analysis.standard.StandardAnalyzer; 58 import org.apache.lucene.document.Document; 59 import org.apache.lucene.queryParser.ParseException; 60 import org.apache.lucene.queryParser.QueryParser; 61 import org.apache.lucene.search.Hits; 62 import org.apache.lucene.search.IndexSearcher; 63 import org.apache.lucene.search.Searcher; 64 65 import org.jibx.runtime.JiBXException; 66 67 import java.io.FileNotFoundException; 68 import java.io.IOException; 69 70 import java.sql.SQLException; 71 72 import java.util.ArrayList; 73 import java.util.Collection; 74 import java.util.Collections; 75 import java.util.Date; 76 import java.util.Iterator; 77 78 import javax.jdo.Extent; 79 import javax.jdo.Query; 80 import javax.jdo.Transaction; 81 82 import javax.servlet.ServletException; 83 84 85 /*** 86 * TODO DOCUMENT ME! 87 * 88 * @author <a href="http://blog.ejb.cn">acai</a> 89 * @version $Revision $ 90 */ 91 public class NewsCMD extends CommonCommand { 92 //~ Static fields/initializers --------------------------------------------- 93 94 /*** 95 * @see com.justhis.control.Command#getName() 96 */ 97 /*** TODO */ 98 private static String indexPath = "index"; 99 100 //~ Instance fields -------------------------------------------------------- 101 102 /*** TODO */ 103 int perPage = 0; 104 105 //~ Methods ---------------------------------------------------------------- 106 107 /*** 108 * TODO 109 * 110 * @return TODO 111 */ 112 public String getName() { 113 return null; 114 } 115 116 /*** 117 * @see com.justhis.control.Command#execute() 118 */ 119 public String execute() 120 throws LogicException, SQLException, ServletException, 121 UtilException { 122 indexPath = warPath + "/WEB-INF/index"; 123 perPage = this.getSystemConfig().getPageSize(); 124 125 if ("AddNews".equals(action)) { 126 addNews(); 127 } else if ("AddNewsForm".equals(action)) { 128 showAddNewsForm(); 129 } else if ("AdminNews".equals(action)) { 130 showNewsList(); 131 } else if ("Default".equals(action)) { 132 showDefaultPage(); 133 } else if ("ShowNews".equals(action)) { 134 return showNewsById(); 135 } else if ("ShowClass".equals(action)) { 136 showClassById(); 137 } else if ("Search".equals(action)) { 138 try { 139 searchNews(); 140 } catch (LuceneException e) { 141 throw new UtilException(e.getMessage(), e); 142 } 143 } 144 145 return null; 146 } 147 148 /*** 149 * @return TODO 150 */ 151 private Collection getClassList() 152 throws UtilException, SQLException, LogicException { 153 NewsSys ns = (NewsSys) JDOUtil.findObjectById(this.getPM(), 154 NewsSys.class, systemOID 155 ); 156 157 return ns.getClassList(); 158 } 159 160 /*** 161 * TODO 162 * 163 * @param oidStr TODO 164 * @param order TODO 165 * 166 * @return TODO 167 * 168 * @throws UtilException TODO 169 * @throws SQLException TODO 170 * @throws LogicException TODO 171 */ 172 private Collection getNewsList(String oidStr, String order) 173 throws UtilException, SQLException, LogicException { 174 Collection c = JDOUtil.findObjList(getPM(), NewsBean.class, order); 175 176 return c; 177 } 178 179 /*** 180 * TODO 181 * 182 * @param oidStr TODO 183 * @param order TODO 184 * @param filter TODO 185 * 186 * @return TODO 187 * 188 * @throws UtilException TODO 189 * @throws SQLException TODO 190 * @throws LogicException TODO 191 */ 192 private Collection getNewsList(String oidStr, String order, String filter) 193 throws UtilException, SQLException, LogicException { 194 Collection c = JDOUtil.findObjList(getPM(), NewsBean.class, order, 195 filter 196 ); 197 198 return c; 199 } 200 201 /*** 202 * @param object 203 * @param string 204 * 205 * @return TODO 206 */ 207 private Collection getNewsListByClassId(String classId, String order) 208 throws SQLException, LogicException { 209 Class newsClass = NewsBean.class; 210 pm = this.getPM(); 211 212 Extent newsBeans = pm.getExtent(newsClass, false); 213 String filter = "newsClassId == classObjId"; 214 String param = "String classObjId"; 215 Query q = pm.newQuery(newsBeans, filter); 216 q.declareParameters(param); 217 q.setOrdering(order); 218 219 Collection newsList = (Collection) q.execute(classId); 220 221 return newsList; 222 } 223 224 /*** 225 * TODO DOCUMENT ME! 226 * 227 * @throws SQLException TODO 228 * @throws LogicException TODO 229 * @throws UtilException TODO 230 * @throws ServletException TODO 231 */ 232 private void addNews() 233 throws SQLException, LogicException, UtilException, 234 ServletException { 235 NewsBean nb = new NewsBean(); 236 237 nb.setKey(xp.getProperty("Key")); 238 239 nb.setSummary(xp.getProperty("Summary")); 240 241 nb.setFrom(xp.getProperty("Source")); 242 nb.setTitleImg(xp.getProperty("TitleImg")); 243 nb.setTopicId(xp.getIntProperty("TopicID")); 244 245 nb.setClassId(xp.getProperty("ClassID")); 246 nb.setNClassId(xp.getProperty("NClassID")); 247 nb.setAuthor(xp.getProperty("Author")); 248 nb.setTitle(xp.getProperty("Title")); 249 nb.setContent(xp.getProperty("Body")); 250 251 nb.setHtml(xp.getBooleanProperty("Htmlable")); 252 System.out.println("HTMLBooean=================" 253 + xp.getBooleanProperty("Htmlable") 254 ); 255 nb.setHeadLine(xp.getBooleanProperty("Headline")); 256 System.out.println("HeadLineBooean=================" 257 + xp.getBooleanProperty("Headline") 258 ); 259 260 nb.setHighlight(xp.getBooleanProperty("HighLight")); 261 262 nb.setDate(new Date()); 263 264 nb.setKeyNewsList(this.findKeyNewsList(xp.getProperty("Key"))); 265 266 Transaction trans = getTransaction(); 267 trans.begin(); 268 269 //ArrayList ans.getClassList(); 270 JDOUtil.createObject(trans, nb); 271 trans.commit(); 272 this.printNewsData(nb); 273 274 /*** 275 * ???????????? 276 */ 277 try { 278 IndexFiles.addDocument(indexPath, 279 warPath + "/xml/" + nb.getObjectId() 280 + ".xml" 281 ); 282 } catch (LuceneException e) { 283 throw new UtilException(e.getMessage(), e); 284 } 285 286 showSuccessNewsForm(); 287 } 288 289 /*** 290 * @param doc 291 * 292 * @return TODO 293 */ 294 private NewsBeanBase doc2NewsBeanBase(Document doc) { 295 NewsBeanBase nb = new NewsBeanBase(); 296 297 nb.setAuthor(doc.get("author")); 298 nb.setTitle(doc.get("title")); 299 nb.setObjectId(doc.get("objectId")); 300 301 //nb.setDate(doc.get("date")); 302 return nb; 303 } 304 305 /*** 306 * @param string 307 * 308 * @return TODO 309 */ 310 private ArrayList findKeyNewsList(String key) 311 throws SQLException, LogicException { 312 pm = this.getPM(); 313 314 Extent newsBeans = pm.getExtent(NewsBean.class, false); 315 String filter = " title==key"; 316 String param = "String key"; 317 Query q = pm.newQuery(newsBeans, filter); 318 q.declareParameters(param); 319 q.setOrdering("date descending"); 320 321 Collection newsList = (Collection) q.execute(key); 322 int size = newsList.size(); 323 324 if (size == 0) { 325 return null; 326 } 327 328 if (size > 10) { 329 size = 10; 330 } else { 331 return new ArrayList(newsList); 332 } 333 334 return new ArrayList(new ArrayList(newsList).subList(0, size)); 335 } 336 337 /*** 338 * 339 */ 340 private void printNewsData(NewsBean nb) throws UtilException { 341 try { 342 JibxUtil.marshallDocument(nb, "../xsl/Show.xsl", 343 warPath + "/xml/" + nb.getObjectId() 344 + ".xml" 345 ); 346 } catch (FileNotFoundException e) { 347 throw new UtilException(e.getMessage(), e); 348 } catch (JiBXException e) { 349 throw new UtilException(e.getMessage(), e); 350 } 351 } 352 353 /*** 354 * TODO DOCUMENT ME! 355 * 356 * @throws LuceneException TODO 357 * @throws ServletException TODO 358 * @throws UtilException TODO 359 */ 360 private void searchNews() 361 throws LuceneException, ServletException, UtilException { 362 Searcher searcher = null; 363 364 try { 365 String keyWords = xp.getProperty("keyword"); 366 Collection c = Collections.synchronizedList(new ArrayList()); 367 368 searcher = new IndexSearcher(indexPath); 369 370 Analyzer analyzer = new StandardAnalyzer(); 371 372 org.apache.lucene.search.Query query = QueryParser.parse("title:\"" 373 + keyWords 374 + "\" " 375 + keyWords, 376 "content", 377 analyzer 378 ); 379 System.out.println("Searching for: " + query.toString()); 380 381 Hits hits = searcher.search(query); 382 System.out.println(hits.length() + " total matching documents"); 383 384 int currentPage = xp.getIntProperty("currentPage"); 385 386 if (currentPage <= 0) { 387 currentPage = 1; 388 } 389 390 PageBean pb = new PageBean(); 391 pb.setRecordSize(hits.length()); 392 pb.setCurrentPage(currentPage); 393 394 int start = (currentPage - 1) * perPage; 395 396 int end = Math.min(hits.length(), start + perPage); 397 398 for (int i = start; i < end; i++) { 399 Document doc = hits.doc(i); 400 401 c.add(doc2NewsBeanBase(doc)); 402 } 403 404 NewsCommonXML ncx = new NewsCommonXML(); 405 ncx.setNewsCollection(c); 406 ncx.setMsg(keyWords); 407 ncx.setPageBean(pb); 408 409 this.go(ncx, "Search.xsl"); 410 } catch (IOException e) { 411 throw new LuceneException(e.getMessage(), e); 412 } catch (ParseException e) { 413 throw new LuceneException(e.getMessage(), e); 414 } finally { 415 if (searcher != null) { 416 try { 417 searcher.close(); 418 } catch (IOException e1) { 419 } 420 } 421 } 422 } 423 424 /*** 425 * TODO DOCUMENT ME! 426 * 427 * @throws ServletException TODO 428 * @throws UtilException TODO 429 * @throws SQLException TODO 430 * @throws LogicException TODO 431 */ 432 private void showAddNewsForm() 433 throws ServletException, UtilException, SQLException, 434 LogicException { 435 NewsSys ns = (NewsSys) JDOUtil.findObjectById(this.getPM(), 436 NewsSys.class, systemOID 437 ); 438 NewsCommonXML ncx = new NewsCommonXML(); 439 440 ncx.setClassCollection(ns.getClassList()); 441 this.go(ncx, "AdminArticleAdd.xsl"); 442 } 443 444 /*** 445 * TODO DOCUMENT ME! 446 * 447 * @throws ServletException TODO 448 * @throws UtilException TODO 449 * @throws SQLException TODO 450 * @throws LogicException TODO 451 */ 452 private void showClassById() 453 throws ServletException, UtilException, SQLException, 454 LogicException { 455 NewsCommonXML ncx = new NewsCommonXML(); 456 String classId = xp.getProperty("objectId"); 457 ncx.setDayHotNews(getNewsListByClassId(classId, "dayHits descending")); 458 ncx.setHotNews(getNewsListByClassId(classId, "hits descending")); 459 460 Collection c = getNewsListByClassId(classId, "date descending"); 461 462 ArrayList newsList = new ArrayList(c); 463 464 int currentPage = xp.getIntProperty("currentPage"); 465 466 if (currentPage <= 0) { 467 currentPage = 1; 468 } 469 470 PageBean pb = new PageBean(); 471 pb.setRecordSize(newsList.size()); 472 pb.setCurrentPage(currentPage); 473 474 int startId = (currentPage - 1) * perPage; 475 newsList = new ArrayList(newsList.subList(startId, 476 ((newsList.size() > (startId 477 + perPage)) 478 ? (startId + perPage) 479 : newsList.size()) 480 ) 481 ); 482 483 ncx.setNewsCollection(newsList); 484 485 //ncx.setClassCollection(this.getClassList()); 486 ncx.setPageBean(pb); 487 this.go(ncx); 488 } 489 490 /*** 491 * TODO DOCUMENT ME! 492 * 493 * @throws UtilException TODO 494 * @throws SQLException TODO 495 * @throws LogicException TODO 496 * @throws ServletException TODO 497 */ 498 private void showDefaultPage() 499 throws UtilException, SQLException, LogicException, 500 ServletException { 501 NewsCommonXML ncx = new NewsCommonXML(); 502 ncx.setDayHotNews(getNewsList(null, "dayHits descending")); 503 ncx.setWeekHotNews(getNewsList(null, "weekHits descending")); 504 ncx.setHotNews(getNewsList(null, "hits descending")); 505 ncx.setHeadLineNews(getNewsList(null, null, "headLine==true")); 506 507 Collection classC = getClassList(); 508 509 //ncx.setClassCollection(classC); 510 Iterator classIt = classC.iterator(); 511 512 while (classIt.hasNext()) { 513 NewsClass nc = (NewsClass) classIt.next(); 514 515 ncx.addNewsCollection(getNewsListByClassId(nc.getObjectId(), 516 "date descending" 517 ) 518 ); 519 } 520 521 this.go(ncx); 522 } 523 524 /*** 525 * TODO DOCUMENT ME! 526 * 527 * @return TODO 528 * 529 * @throws ServletException TODO 530 * @throws UtilException TODO 531 * @throws SQLException TODO 532 * @throws LogicException TODO 533 */ 534 private String showNewsById() 535 throws ServletException, UtilException, SQLException, 536 LogicException { 537 NewsCommonXML ncx = new NewsCommonXML(); 538 Transaction trans = getTransaction(); 539 trans.begin(); 540 541 NewsBean nb = (NewsBean) JDOUtil.findObjectById(trans, NewsBean.class, 542 xp.getProperty("objectId") 543 ); 544 nb.setHits(nb.getHits() + 1); 545 546 System.out.println("News now hits is " + nb.getHits()); 547 ncx.setNews(nb); 548 trans.commit(); 549 550 String path = "/xml/" + nb.getObjectId() + ".xml"; 551 System.out.println(path); 552 553 //this.printNewsData(nb); 554 return path; 555 556 //this.go(ncx); 557 } 558 559 /*** 560 * TODO DOCUMENT ME! 561 * 562 * @throws ServletException TODO 563 * @throws UtilException TODO 564 * @throws SQLException TODO 565 * @throws LogicException TODO 566 */ 567 private void showNewsList() 568 throws ServletException, UtilException, SQLException, 569 LogicException { 570 NewsCommonXML ncx = new NewsCommonXML(); 571 572 ncx.setNewsCollection(JDOUtil.findObjList(this.getPM(), NewsBean.class)); 573 this.go(ncx); 574 } 575 576 /*** 577 * TODO 578 * 579 * @throws ServletException TODO 580 * @throws UtilException TODO 581 * @throws SQLException TODO 582 * @throws LogicException TODO 583 */ 584 private void showSuccessNewsForm() 585 throws ServletException, UtilException, 586 SQLException, LogicException { 587 NewsSys ns = (NewsSys) JDOUtil.findObjectById(this.getPM(), 588 NewsSys.class, systemOID 589 ); 590 NewsCommonXML ncx = new NewsCommonXML(); 591 ncx.setMsg("Successful!"); 592 ncx.setClassCollection(ns.getClassList()); 593 this.go(ncx, "AdminArticleAdd.xsl"); 594 } 595 } 596 597 598 /* 599 * $Log: NewsCMD.java,v $ 600 * Revision 1.1 2003/09/10 09:28:37 bitiboy 601 * *** empty log message *** 602 * 603 * 604 */

This page was automatically generated by Maven